package com.monitor.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.monitor.dao.LocationDao;
import com.monitor.po.Location;
import com.monitor.util.Pager;

@SuppressWarnings("unchecked")
public class LocationDaoImpl implements LocationDao {
	private SessionFactory sessionFactory;

	@Override
	public Pager<Location> getLocationByUid(long uid, int pageSize, int currentPage) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from Location l where l.ownerUid=:ownerUid").setLong("ownerUid", uid);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
			List<Location> list = sesion.createQuery("from Location l where l.ownerUid=:ownerUid order by l.id desc").setLong("ownerUid", uid).setFirstResult((currentPage - 1) * pageSize)
					.setMaxResults(pageSize).list();
			Pager<Location> pager = new Pager<Location>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting getLocationByUid()....");
		}
	}

	@Override
	public void addLocation(Location location) throws Exception {
		try {
			sessionFactory.getCurrentSession().persist(location);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting addLocation()....");
		}
	}

	@Override
	public int deleteLocationByUid(long uid) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from Location l where l.ownerUid=:ownerUid").setLong("ownerUid", uid).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting deleteLocationByUid()....");
		}
	}

	@Override
	public int deleteLocation(long uid, long lid) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from Location l where l.id=:id and l.ownerUid=:ownerUid").setLong("id", lid).setLong("ownerUid", uid).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting deleteLocation()....");
		}
	}

	@Override
	public int deleteLocation(long uid, long[] lids) throws Exception {
		try {
			StringBuffer sql = new StringBuffer("delete from Location l where l.ownerUid=? and (");
			int length = lids.length;
			for (int i = 0; i < length; i++) {
				sql.append(" l.id=? ");
				if (i != length - 1) {
					sql.append(" or ");
				}
			}
			sql.append(")");
			Query query = sessionFactory.getCurrentSession().createQuery(sql.toString());
			query.setLong(0, uid);
			for (int i = 0; i < length; i++) {
				long id = lids[i];
				query.setLong(i + 1, id);
			}
			return query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting deleteLocation()....");
		}
	}
	
	@Override
	public int clearThreeDayAgoLocation() throws Exception {
		try {
			long oneMonthAgo = System.currentTimeMillis() - 3 * 24 * 60 * 60 * 1000;
			return sessionFactory.getCurrentSession().createQuery("delete from Location l where l.serverAddTime<:time").setLong("time", oneMonthAgo).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("exception occured when excuting clearThreeDayAgoLocation()....");
		}
	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
